package com.ruoyi.btlfbreed.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.btlfbreed.domain.TbChickenHatchRecords;
import com.ruoyi.btlfbreed.service.ITbChickenHatchRecordsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 肉鸡孵化记录Controller
 * 
 * @author cpdeman
 * @date 2024-05-05
 */
@RestController
@RequestMapping("/btlfbreed/chickenHatchRecords")
public class TbChickenHatchRecordsController extends BaseController
{
    @Autowired
    private ITbChickenHatchRecordsService tbChickenHatchRecordsService;

    /**
     * 查询肉鸡孵化记录列表
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:chickenHatchRecords:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbChickenHatchRecords tbChickenHatchRecords)
    {
        startPage();
        List<TbChickenHatchRecords> list = tbChickenHatchRecordsService.selectTbChickenHatchRecordsList(tbChickenHatchRecords);
        return getDataTable(list);
    }

    /**
     * 导出肉鸡孵化记录列表
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:chickenHatchRecords:export')")
    @Log(title = "肉鸡孵化记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbChickenHatchRecords tbChickenHatchRecords)
    {
        List<TbChickenHatchRecords> list = tbChickenHatchRecordsService.selectTbChickenHatchRecordsList(tbChickenHatchRecords);
        ExcelUtil<TbChickenHatchRecords> util = new ExcelUtil<TbChickenHatchRecords>(TbChickenHatchRecords.class);
        util.exportExcel(response, list, "肉鸡孵化记录数据");
    }

    /**
     * 获取肉鸡孵化记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:chickenHatchRecords:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbChickenHatchRecordsService.selectTbChickenHatchRecordsById(id));
    }

    /**
     * 新增肉鸡孵化记录
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:chickenHatchRecords:add')")
    @Log(title = "肉鸡孵化记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbChickenHatchRecords tbChickenHatchRecords)
    {

        tbChickenHatchRecords.setHatchRate((double)Math.round(tbChickenHatchRecords.getHatchedChicks()/(double) tbChickenHatchRecords.getTotalEggs()*100));
        tbChickenHatchRecords.setMortalityRate(Math.round((double) tbChickenHatchRecords.getDeadEggs()/(double) tbChickenHatchRecords.getTotalEggs()*100));

        return toAjax(tbChickenHatchRecordsService.insertTbChickenHatchRecords(tbChickenHatchRecords));
    }

    /**
     * 修改肉鸡孵化记录
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:chickenHatchRecords:edit')")
    @Log(title = "肉鸡孵化记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbChickenHatchRecords tbChickenHatchRecords)
    {
        tbChickenHatchRecords.setHatchRate(Math.round(((double) tbChickenHatchRecords.getHatchedChicks()/(double) tbChickenHatchRecords.getTotalEggs())*100));
        tbChickenHatchRecords.setMortalityRate(Math.round((double) tbChickenHatchRecords.getDeadEggs()/(double) tbChickenHatchRecords.getTotalEggs()*100));

        return toAjax(tbChickenHatchRecordsService.updateTbChickenHatchRecords(tbChickenHatchRecords));
    }

    /**
     * 删除肉鸡孵化记录
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:chickenHatchRecords:remove')")
    @Log(title = "肉鸡孵化记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbChickenHatchRecordsService.deleteTbChickenHatchRecordsByIds(ids));
    }
}
